package com.cloudshop.service.user.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * WebSecurity 配置类.
 *
 * @author 宫新程
 * @since 2019/4/1 13:38
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfigBean extends WebSecurityConfigurerAdapter {

  /**
   * authorizeRequests() -> 所有security全注解配置实现的开端，表示开始说明需要的权限，需要的权限需要两部分，
   * 1：拦截的路径， 2：访问该路径需要的权限
   * antMatchers() -> 表示拦截路径
   * permitAll() -> 任何权限都可以访问（通行）
   * anyRequest() -> 任何的请求
   * authenticated() -> 认证后才能访问
   * and().csrf().disable() -> 固定写法，表示使 csrf 拦截失效.
   */
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/**")
        .permitAll()
        .anyRequest()
        .authenticated()
        .and()
        .csrf()
        .disable();
  }

  @Bean
  public BCryptPasswordEncoder encoder() {
    return new BCryptPasswordEncoder();
  }
}
